package common.list;

import java.util.Scanner;

public class LinkedList<T> {

    class Node<T>{
        T data;
        Node<T> next;

        public Node(T data, Node<T> next) {
            this.data = data;
            this.next = next;
        }
    }

    private Node<T> header = null;
    private int size = 0;

    /**
     * Main method
     * @param args
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        LinkedList<String> linkedList = new LinkedList<>();
        linkedList.add("Jack feell off his bicycle and got hurt. A beautiful young nurse asked him to fill forms. Jack finished them and gave them back.\"Anything else?\" The nurse asked. \"Yes,\"Jack thinks for a while and said,\"I'm a bachelor.\"");
        linkedList.add("Wife:You see.According to te statistics on thepaper,80% of those who have died of liver cancer have drunk alcohol.\n" +
                "Husband:It's okey. To my investigation,all Thespeopleeat meals.");
        linkedList.add(" \"Excuse me,but the seat you've taken is mine.\"\n" +
                "\"Yours?Can you prove it?\"\n" +
                "\"Yes,I put a cup of ice cream on it.\"");
        linkedList.add("One day,Eve asked Adam,\"Doyou really love me?\"\n" +
                "Adam said helplessly,\"Do I have any other choice?\"");

        linkedList.add("Dentist: Please stop howling. I haven\"t even touched your tooth yet.\n" +
                "Patient: I know, but you are standing on my foot!");

        while (true) {
            System.out.print("Input index (0~5): ");
            int integer = scanner.nextInt();
            System.out.println(linkedList.get(integer));
        }

    }

    public void add(T data) {
        if (header == null) {
            header = new Node<>(data, null);
        } else {
            Node<T> tmp = header;
            for (int i = 1; i < size; i++) {
                tmp = tmp.next;
            }
            tmp.next = new Node<>(data, null);
        }
        size++;
    }

    public T get(int index) {
        index = index - 1;
        if (index >= size || index < 0) {
            return null;
        }

        Node<T> tmp = header;
        for (int i = 0; i < index; i++) {
            tmp = tmp.next;
        }
        return tmp.data;
    }
}
